/*

i=a[j]前面最近相同数字的下标
dp[j][0]=dp[i+1][0]+same_color[j]-same_color[i]+a[j]
    , dp[j-1][0]

*/
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
using ll = int64_t;

const ll maxn=2e6+5;
ll n,a[maxn],sc[maxn],si[maxn],dp[maxn];

#define printf

static inline void solve(){
    std::cin>>n;
    for(ll i=0;i<maxn;i++){
        si[i]=0;
        dp[i]=0;
    }
    for(ll i=1;i<=n;i++){
        std::cin>>a[i];
        sc[i]=sc[i-1]+(a[i]==a[i-1]?a[i]:0);
        printf("sc[%lld]=%lld\n",i,sc[i]);
    }
    for(ll j=1;j<=n;j++){
        const ll i=si[a[j]];
        dp[j]=dp[j-1];
        if(i>0){
            dp[j]=std::max(
                dp[i+1]+(sc[j]-sc[i+1])/* 为什么这里不能是sc[j-1]-sc[i+1] */+a[j],
                dp[j]
            );
        }
        printf("i=%lld, dp[%lld]=%lld\n",i,j,dp[j]);
        si[a[j]]=j;
    }
    std::cout<<dp[n]<<"\n";
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    ll T;
    std::cin>>T;
    while(T--)solve();
}